/*
 * Copyright (c) 2021 HPMicro
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#include "board.h"
#include "hpm_gpio_drv.h"

#include "los_tick.h"
#include "los_task.h"
#include "los_config.h"
#include "los_interrupt.h"
#include "los_debug.h"
#include "los_compiler.h"

#include "uart.h"
extern void __libc_init_array(void);

VOID taskSampleEntry2(VOID)
{
    while(1) {
      LOS_TaskDelay(10000);
      //printf("liteos taskSampleEntry2 running...\n");
    }
}


VOID taskSampleEntry1(VOID)
{
    while(1) {
      LOS_TaskDelay(20000);
      //printf("liteos taskSampleEntry1 running...\n");
    }

}

UINT32 taskSample(VOID)
{
    UINT32  uwRet;
    UINT32 taskID1,taskID2;
    TSK_INIT_PARAM_S stTask1={0};
    stTask1.pfnTaskEntry = (TSK_ENTRY_FUNC)taskSampleEntry1;
    stTask1.uwStackSize  = 0X1000;
    stTask1.pcName       = "taskSampleEntry1";
    stTask1.usTaskPrio   = 6;
    uwRet = LOS_TaskCreate(&taskID1, &stTask1);
    if (uwRet != LOS_OK) {
        printf("create task1 failed\n");
    }

    stTask1.pfnTaskEntry = (TSK_ENTRY_FUNC)taskSampleEntry2;
    stTask1.uwStackSize  = 0X1000;
    stTask1.pcName       = "taskSampleEntry2";
    stTask1.usTaskPrio   = 7;
    uwRet = LOS_TaskCreate(&taskID2, &stTask1);
    if (uwRet != LOS_OK) {
        printf("create task2 failed\n");
    }
    return LOS_OK;
}

int main(void)
{
    UINT32 ret;
    board_init();
    //UartInit();
    //Uart0RxIrqRegister();
    board_print_banner();
    board_print_clock_freq();
    board_ungate_mchtmr_at_lp_mode();
    //USART_Config();

    printf("\n\rhello world!!\n\r");

    ret = LOS_KernelInit();
     if (ret != LOS_OK) {
        printf("LiteOS kernel init failed! ERROR: 0x%x\n", ret);
        goto START_FAILED;
    }


    
    //HalPlicInit();
    
    //__libc_init_array();
    taskSample();
#if (LOSCFG_USE_SHELL == 1)
    ret = LosShellInit();
    if (ret != LOS_OK) {
        printf("LosShellInit failed! ERROR: 0x%x\n", ret);
    }
#endif
    if (ret == LOS_OK) {
        LOS_Start();
    }

START_FAILED:
    while (1) {
        __asm volatile("wfi");
    }
}
